package com.green.common.fusioncharts;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
 * Fusioncharts曲线
 * @author yinyuqiao
 * @time 2013-1-7 上午11:22:26
 */
public class YieldChart {

	private Points[] strsPoint;
	private Map<String, Points[]> map;
	private List<ChartXLabel> label;
	private StringBuffer buffer;
	private double maxX;
	private double minX;
	private double maxY;
	private double minY;
						
	private String caption = null; // 图的名字
	private String yAxisName = null; // y轴名称 此处中文不能正确显示
	private String xAxisName = null; // x轴名称
	private String numDivLines = null; // 此处表示 纵坐标分成的分数
	private String decimalPrecision = "0"; //小数点位数
	private String divLineDecimalPrecision = null; //Y轴小数点位数
	
	@SuppressWarnings("unused")
	private YieldChart(){
		
	}
	
	public YieldChart(String caption , String yAxisName ,String xAxisName ,String numDivLines,String decimalPrecision,String divLineDecimalPrecision){
		this.caption = caption ;
		this.yAxisName = yAxisName ;
		this.xAxisName = xAxisName ;
		this.numDivLines = numDivLines ;
		this.decimalPrecision = decimalPrecision;
		this.divLineDecimalPrecision = divLineDecimalPrecision;
	}
	/**
	 * 核心存款
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getCoreDepositChartStrs() {
		buffer = new StringBuffer();
		setXY();
		try {
			buffer   //labelStep=\"8\"每隔8点显示一个label
					.append("<chart palette=\"2\" exportEnabled=\"1\" numVDivLines=\"5\" exportAtClient=\"1\" exportHandler=\"fcExporter1\" caption=\"" // chart的属性
							+ caption
							+ "\" numDivLines=\""
							+ numDivLines
							+ "\" decimalPrecision=\""
							+ decimalPrecision
							+ "\" divLineDecimalPrecision=\""
							+ divLineDecimalPrecision
							+ "\" yAxisName=\""
							+ yAxisName   
							+ "\" xAxisName=\""
							+ xAxisName
							+ "\" rotateNames=\"0\" showLegend=\"1\" showNames=\"1\" xAxisMaxValue=\""
							+ maxX + "\" xAxisMinValue=\""
							+ minX
//							+ "\" yAxisMaxValue=\"" + maxY
//							+ "\" yAxisMinValue=\"0\""
							+ "\" formatNumber=\"1\" baseFontSize=\"13\" bgColor=\"321456,cccccc\" bgAlpha=\"20,10\" >");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		buffer.append(getLabel()); // chart的横坐标X轴的分隔线
		
		for (Iterator it = (Iterator) map.entrySet().iterator(); it.hasNext();) {
			Map.Entry<String, Points[]> e = (Map.Entry<String, Points[]>) it.next();
			
			Points[] list = (Points[]) e.getValue();
			String name  =  e.getKey();
			//给曲线固定颜色
			buffer.append(getXLine1(list,name));
//			if(name.equals("输入曲线")){
//				buffer.append(getXLine(list , name,11));
//			}
//			if(name.equals("输出曲线")){
//				buffer.append(getXLine(list , name,12));
//			}
//			if(name.equals("输出参数0")){
//				buffer.append(getXLine(list , name,21));
//			}
//			if(name.equals("输出参数1")){
//				buffer.append(getXLine(list , name,22));		
//			}
//			if(name.equals("输出参数2")){
//				buffer.append(getXLine(list , name,23));
//			}
//			if(name.equals("输出参数3")){
//				buffer.append(getXLine(list , name,24));
//			}
//			if(name.equals("输出参数4")){
//				buffer.append(getXLine(list , name,25));
//			}
			
		}
		
		buffer.append("</chart>");
		return buffer.toString();
	}
	
	/**
	 * 整体绩效
	 */
	@SuppressWarnings("unchecked")
	public String getChartStrs() {
		buffer = new StringBuffer();
		setXY();
		try {
			buffer
					.append("<chart palette=\"2\" exportEnabled=\"1\" exportAtClient=\"1\" exportHandler=\"fcExporter1\" caption=\"" // chart的属性
							+ caption
							+ "\" numDivLines=\""
							+ numDivLines
							+ "\" decimalPrecision=\""
							+ decimalPrecision
							+ "\" divLineDecimalPrecision=\""
							+ divLineDecimalPrecision
							+ "\" yAxisName=\""
							+ yAxisName   
							+ "\" xAxisName=\""
							+ xAxisName
							+ "\" rotateNames=\"0\" showLegend=\"1\" showNames=\"1\" xAxisMaxValue=\""
							+ maxX + "\" xAxisMinValue=\""
							+ minX
							+ "\" yAxisMaxValue=\"" + maxY
							+ "\" yAxisMinValue=\""
							+ minY + "\" formatNumber=\"1\" baseFontSize=\"13\" bgColor=\"321456,cccccc\" bgAlpha=\"20,10\" >");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		buffer.append(getLabel()); // chart的横坐标X轴的分隔线
		
		for (Iterator it = (Iterator) map.entrySet().iterator(); it.hasNext();) {
			Map.Entry<String, Points[]> e = (Map.Entry<String, Points[]>) it.next();
			
			Points[] list = (Points[]) e.getValue();
			String name  =  e.getKey();
			buffer.append(this.getXLine(list, name));
		}
		buffer.append("</chart>");
		return buffer.toString();
	}
	@SuppressWarnings("unchecked")
	public void setXY() { // 设置x y 轴的坐标
		minX = minY = 0.0;
		maxX = maxY = 1.0;
		if (map != null) {
			for (Iterator it = (Iterator) map.entrySet().iterator(); it.hasNext();) {
				Map.Entry<String, Points[]> e = (Map.Entry<String, Points[]>) it.next();
				
				Points[] list = (Points[]) e.getValue();
				@SuppressWarnings("unused")
				String name  =  e.getKey();
				
				for (int i = 0; i < list.length; i++) {
					double tempX = list[i].getX();
					double tempY = list[i].getY();
					if (tempX > maxX) {
						maxX = tempX;
					}else if(tempX < minX){
						minX = tempX;
					}
					if (tempY > maxY) {
						maxY = list[i].getY();
					}else if(tempY < minY){
						minY = tempY;
					}
				}
			}
		} else {
			
		}
		maxY = Math.rint(maxY*4/3);
		minY = Math.rint(minY*3);
		@SuppressWarnings("unused")
		double x = maxX - minX ;
		@SuppressWarnings("unused")
		double y = maxY - minY ;
	}
	public void setXY1() { // 设置x y 轴的坐标	
		maxY = Math.rint(10);
		minY = Math.rint(0);
	}
	private String getLabel() {// chart的横坐标X轴的分隔线
		StringBuffer temp = new StringBuffer();
		temp.append("<categories verticalLineColor=\"888888\" verticalLineThickness=\"1\">");
		for (ChartXLabel obj : label) {
			temp.append("<category label=\"");
			temp.append(obj.getName());
			temp.append("\" x=\"");
			temp.append(obj.getValue());
			temp.append("\" showVerticalLine=\"1\" />");
		}
		temp.append("</categories>");
		return temp.toString();
	}

	private String getXLine(Points[] point, String lineNum) {// chart的 所有的用户点连接成的曲线
		StringBuffer temp = new StringBuffer();
		
		double x = Math.random()*16777214;
		String str = Integer.toHexString((int)x);
		
		temp.append("<dataSet seriesName=\""
						+ lineNum
						+ "\" drawLine=\"1\" color=\""+str+"\" anchorSides=\"20\" anchorRadius=\"1\" anchorBgColor=\"ffffff\" anchorBorderColor=\""+str+"\">");
		for (int i = 0; point.length > i; i++) {
			if (point[i] != null)
				temp.append("<set y=\"" + point[i].getY() + "\" x=\""
						+ point[i].getX() + "\" />");
		}
		temp.append("</dataSet>");
		return temp.toString();
	}

	private String getXLine1(Points[] point, String lineNum) {// chart的 所有的用户点连接成的曲线
		StringBuffer temp = new StringBuffer();
		
		double x = Math.random()*16777214;
		String str = Integer.toHexString((int)x);
		
		temp.append("<dataSet seriesName=\""
						+ lineNum
						+ "\" drawLine=\"1\" color=\""+str+"\" anchorSides=\"20\" anchorRadius=\"1\" anchorBgColor=\"ffffff\" anchorBorderColor=\""+str+"\">");
		for (int i = 0; point.length > i; i++) {
			if (point[i] != null)
				temp.append("<set y=\"" + point[i].getY() + "\" x=\""
						+ point[i].getX() + "\" />");
		}
		temp.append("</dataSet>");
		return temp.toString();
	}
	//重载getXLine
	//设置每条曲线的颜色
	@SuppressWarnings("unused")
	private String getXLine(Points[] point, String lineNum,int num) {// chart的 所有的用户点连接成的曲线
		StringBuffer temp = new StringBuffer();
		String str = "";
		if(num == 11){
			str = "030303";
		}
		if(num == 12){
			str = "3B3B3B";
		}
		if(num == 21){
			str = "5C94CD";
		}
		if(num == 22){
			str = "4876FF";
		}
		if(num == 23){
			str = "4169E1";
		}
		if(num == 24){
			str = "0000CD";
		}
		if(num == 25){
			str = "000080";
		}
		temp.append("<dataSet seriesName=\""
						+ lineNum
						+ "\" drawLine=\"1\" color=\""+str+"\" anchorSides=\"20\" anchorRadius=\"1\" anchorBgColor=\"ffffff\" anchorBorderColor=\""+str+"\">");
		for (int i = 0; point.length > i; i++) {
			if (point[i] != null)
				temp.append("<set y=\"" + point[i].getY() + "\" x=\""
						+ point[i].getX() + "\" />");
		}
		temp.append("</dataSet>");
		return temp.toString();
	}
	
	public Points[] getStrsPoint() {
		return strsPoint;
	}

	public void setStrsPoint(Points[] strsPoint) {
		this.strsPoint = strsPoint;
	}

	public Map<String, Points[]> getMap() {
		return map;
	}

	public void setMap(Map<String, Points[]> map) {
		this.map = map;
	}

	public void setLabel(List<ChartXLabel> label) {
		this.label = label;
	}

}
